<template>
  <div>
    <el-card class="box-card" style="margin-top: 0;">
      <div slot="header" class="clearfix">
        <span>数据列表</span>


        <el-button
          type="primary"
          size="mini"
          style="float: right; margin-left: 10px;"
          @click="handleExport"
        >导出全量表
        </el-button>
        <el-button
          type="primary"
          size="mini"
          style="float: right; margin-left: 10px;"
          @click="handleDelete(1)"
          v-hasPermi="['campSystem:campManager:agency:remove']"
        >批量删除
        </el-button>
        <el-button
          type="primary"
          size="mini"
          style="float: right; margin-left: 10px;"
          @click="handleAdd"
          v-hasPermi="['campSystem:campManager:agency:add']"
        >添加
        </el-button>
      </div>
      <div class="text item">
        <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" align="center" width="55"/>
          <el-table-column label="姓名" align="center" prop="name"/>
          <el-table-column label="地址" align="center" min-width="150" prop="visitUrl">
            <template slot-scope="scope">
              <el-tooltip effect="dark" content="点击复制" placement="top-end">
              <span class="copy-btn" v-clipboard:copy="scope.row.visitUrl"
                    v-clipboard:success="onCopy"
                    v-clipboard:error="onError">{{scope.row.visitUrl}}</span>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" min-width="100">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                v-hasPermi="['campSystem:campManager:agency:update']"
                @click="handleEdit(scope.row)"
              >编辑
              </el-button>
              <el-button
                size="mini"
                type="text"
                class="btn_danger"
                v-hasPermi="['campSystem:campManager:agency:remove']"
                @click="handleDelete(2, scope.row)"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!--编辑用户信息弹框-->
    <agency-form
      :dialogVisibleFu="dialogVisibleEditFu"
      :formDataFu="formDataFu"
      @closeDialog="closeDialogEdit"
    ></agency-form>
  </div>
</template>

<script>
  import {GetAgencyList, RemoveAgency} from "@/api/campSystem/campOper";
  import CheckForm from "@/components/CampSystem/CampOper/Check/CheckForm";
  import AgencyForm from "@/components/CampSystem/CampOper/Agency/AgencyForm";

  export default {
    name: "AgencyList",
    components: {CheckForm, AgencyForm},
    props: {
      idFu: [String, Number]
    },
    data() {
      return {
        loading: false,// 表格加载loading
        queryParams: {
          page: 1,
          pageSize: 10,
          campId: null
        },
        total: 0,// 数据总条数
        tableList: [], // 表格数据列表
        dialogVisibleEditFu: false,// 控制编辑用户信息弹框的显示隐藏
        formDataFu: {
          name: null,
          campId: null
        },
        uuids: '', //删除操作uuid集合
      };
    },
    created() {
      this.queryParams.campId = this.idFu;
      this.getList();
    },
    methods: {
      // 复制成功时的回调函数
      onCopy(e) {
        this.$message.success("链接已复制到剪切板！")
      },
      // 复制失败时的回调函数
      onError(e) {
        this.$message.error("抱歉，复制失败！")
      },
      // 关闭编辑经销商信息弹框，子组件触发
      closeDialogEdit(val, type) {
        this.dialogVisibleEditFu = val;
        if (type) {
          this.getList();
        }
      },
      // 添加按钮
      handleAdd() {
        console.log(this.campId)
        this.formDataFu = {
          name: null,
          campId: this.idFu
        };
        this.dialogVisibleEditFu = true;
      },
      // 编辑按钮
      handleEdit(data) {
        this.formDataFu = {
          name: data.name,
          campId: data.campId,
          uuid: data.uuid
        };
        this.dialogVisibleEditFu = true;
      },
      // 删除按钮
      handleDelete(type, data) {
        let uuids;
        if (type == 1) {
          if (!this.uuids) {
            return this.msgInfo('请选择需要删除的经销商')
          }
          uuids = this.uuids
        } else {
          uuids = data.uuid
        }
        this.$confirm('是否确认删除该经销商?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          RemoveAgency(uuids)
            .then(response => {
              console.log(response);
              if (response.code != 200) {
                return this.msgError(response.msg);
              }
              this.msgSuccess(response.msg);
              this.getList();
            })
            .catch(error => {
              console.log(error);
            });
        }).catch(() => {
          this.msgInfo('已取消删除')
        });
      },
      // 批量选择
      handleSelectionChange(selection) {
        this.uuids = selection.map(item => {
          return item.uuid
        }).join(',');
      },
      // 获取列表
      getList() {
        this.loading = true;
        console.log(this.queryParams)
        GetAgencyList(this.queryParams)
          .then(response => {
            console.log(response);
            if (response.code != 200) {
              this.loading = false;
              return this.msgError(response.msg);
            }
            this.tableList = response.rows;
            this.total = response.total;
            this.loading = false;
          })
          .catch(error => {
            console.log(error);
            this.loading = false;
          });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download(
          "/modules-campsite/distribution/export",
          {
            campsiteId: this.queryParams.campId
          },
          `经销商全量表.xlsx`
        );
      },
    },
    watch: {
      idFu(val) {
        this.queryParams.campId = val;
        this.getList();
      },
    }
  }
</script>

<style lang="scss">
  @import "@/assets/styles/card.scss";

  .copy-btn {
    &:hover {
      cursor: pointer;
      color: #4D75F3;
      text-decoration: underline;
    }
  }
</style>


